<?php
/**
 * User: hjd-yao
 * Date: 2019/4/24
 * Time: 17:58
 */

namespace App\Http\Controllers\Shop;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\Shop\ShopGoodsModel;
use App\Models\Shop\ShopGoodsSpecValueModel;
use App\Models\Shop\ShopSpecModel;
use App\Models\Shop\ShopTypeModel;
use Illuminate\Http\Request;

class ShopSpecController extends BackendController
{
    /**
     * @title 商品规格列表
     * @param Request $request
     * @return View
     */
    public function specs(Request $request){
        $type_id = $request->get('type',0);
        $specModel = ShopSpecModel::with('type')->has('type');
        if($type_id){
            $specModel = $specModel->where('type_id',$type_id);
        }
        $list = $specModel->paginate($this->getPerPageNum());
        return view('backend::shop.type.specs_list',[
            'page_title'=>'规格列表',
            'type_id'=>$type_id,
            'list'=>$list
        ]);
    }

    /**
     * @title 添加商品规格
     * @param Request $request
     * @return SelfResponse|View
     */
    public function create(Request $request)
    {
        if($request->isMethod('post')){
            $specModel = new ShopSpecModel();
            $specModel->type_id = $request->post('goods_type');
            $specModel->title = $request->post('title');
            $specModel->value = arr2str(str2arr($request->post('value')));
            if(!$specModel->save()){
                return SelfResponse::error('添加失败');
            }
            return SelfResponse::success('添加成功','b_ShopSpec_specs');
        }else{
            return view('backend::shop.type.spec_add_or_edit',[
                'page_title'=>'添加规格',
                'type'=>$request->get('type',1),
                'goods_types'=>ShopTypeModel::init()->getNames()
            ]);
        }
    }

    /**
     * @title 编辑商品规格
     * @param Request $request
     * @param $id
     * @return SelfResponse|View
     */
    public function update(Request $request, $id)
    {
        $info = ShopSpecModel::find($id);
        if($request->isMethod('post')){
            $info->type_id = $request->post('goods_type');
            $info->title = $request->post('title');
            $info->value = arr2str(str2arr($request->post('value')));
            if(!$info->save()){
                return SelfResponse::error('编辑失败');
            }
            return SelfResponse::success('编辑成功','b_ShopSpec_specs');
        }else{
            return view('backend::shop.type.spec_add_or_edit',[
                'page_title'=>'编辑'.$info->title,
                'goods_types'=>ShopTypeModel::init()->getNames(),
                'info'=>$info
            ]);
        }
    }

    /**
     * @title 删除商品规格
     * @param $id
     * @return SelfResponse|RedirectResponse
     */
    public function delete($id)
    {
        $info = ShopSpecModel::find($id);
        if(!$info){
            return SelfResponse::error('删除信息不存在!');
        }

        if(!$info->delete()){
            return SelfResponse::error('删除失败');
        }
        return SelfResponse::success('删除成功','b_ShopSpec_specs');
    }

    /**
     * @title 批量删除商品规格
     * @param Request $request
     * @return SelfResponse
     */
    public function batchDelete(Request $request)
    {
        $ids = array_filter($request->input('ids'));
        if (!$ids){
            return SelfResponse::error('请选择要删除的数据');
        }

        if(!ShopSpecModel::whereIn('id',$ids)->delete()){
            return SelfResponse::error('批量删除失败');
        }
        return SelfResponse::success('批量删除成功','b_ShopSpec_specs');
    }

    /**
     * @title 获取规格数据
     * @param Request $request
     * @return JsonResponse
     */
    public function getSpecByTypeId(Request $request)
    {
        $type_id = $request->get('goods_type');
        $specs =  ShopSpecModel::where('type_id',$type_id)->select(['id','title','value'])->get()->toArray();
        return response()->json($specs);
    }

    /**
     * @title 商品sku信息
     * @param Request $request
     * @param $goods_id
     * @return View
     */
    public function getSKUByGoodsId(Request $request,$goods_id)
    {
        if($request->isMethod('post')){
            $prices = $request->post('price');
            $stocks = $request->post('stock');
            $vip_prices = $request->post('vip_price');
            $market_price = $request->post('market_price');
            foreach ($prices as $id=>$price){
                ShopGoodsSpecValueModel::where('id',$id)->update(['price'=>$price,'vip_price'=>$vip_prices[$id],'market_price'=>$market_price[$id],'stock'=>$stocks[$id]]);
            }
            return SelfResponse::success('编辑成功','b_ShopGoods_goods');
        }else{
            $goods = ShopGoodsModel::find($goods_id);
            return view('backend::shop.type.spec_sku_list',[
                'page_title'=>'货品信息',
                'goods'=>$goods
            ]);
        }
    }

    public function editSku()
    {

    }

}